package com.shiwenping.reply;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.shiwenping.reply.adapter.MainFragmentAdapter;
import com.shiwenping.reply.databinding.ActivityMainBinding;
import com.shiwenping.reply.fragment.BaseFragment;
import com.shiwenping.reply.service.ToastService;
import com.shiwenping.reply.tools.Path;
import com.shiwenping.reply.tools.Tools;
import com.shiwenping.reply.tools.sp.SettingInstance;
import com.shiwenping.reply.tools.sp.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import ezy.assist.compat.SettingsCompat;

import static android.support.design.widget.FloatingActionButton.SIZE_NORMAL;
import static com.shiwenping.reply.tools.Path.KEY_IS_TOAST;
import static com.shiwenping.reply.tools.Path.KEY_IS_TOAST_LEFT;

/**
 * @author shiwenping
 */
@Route(path = Path.MAIN_PATH)
public class MainActivity extends BaseActivity {

    /**
     * 碎片列表
     */
    private List<BaseFragment> fragments = new ArrayList<>();

    /**
     * Fragment　适配器
     */
    private MainFragmentAdapter mainFragmentAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        Tools.toastShow(this, SharedPreferenceUtil.get(this, KEY_IS_TOAST, true));

        SettingInstance.getInstance(this).getSharedPreferences().registerOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);

        initFragment();
    }

    private SharedPreferences.OnSharedPreferenceChangeListener onSharedPreferenceChangeListener =
            (sharedPreferences, s) -> {
                if (s.equals(KEY_IS_TOAST)) {
                    Tools.toastShow(getActivity(), sharedPreferences.getBoolean(s, true));
                } else if (s.equals(KEY_IS_TOAST_LEFT)) {
                    Intent intent = new Intent(ToastService.TOAST_IS_LEFT);
                    intent.putExtra("isLeft", sharedPreferences.getBoolean(s, true));
                    sendBroadcast(intent);
                }
            };

    /**
     * 初始化碎片
     */
    private void initFragment() {
        fragments.add((BaseFragment) ARouter.getInstance().build(Path.MAIN_INDEX).withBoolean("isFile",false).navigation());
        fragments.add((BaseFragment) ARouter.getInstance().build(Path.MAIN_INDEX).withBoolean("isFile",true).navigation());
        fragments.add((BaseFragment) ARouter.getInstance().build(Path.MAIN_SETTING).navigation());
        mainFragmentAdapter = new MainFragmentAdapter(getSupportFragmentManager(), fragments);
        ((ViewPager)findViewById(R.id.viewPager)).setAdapter(mainFragmentAdapter);

        ((BottomNavigationView)findViewById(R.id.bottom)).setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.main_bottom_index:
                    ((ViewPager)findViewById(R.id.viewPager)).setCurrentItem(0, false);
                    break;
                case R.id.main_bottom_file:
                    ((ViewPager)findViewById(R.id.viewPager)).setCurrentItem(1, false);
                    break;
                case R.id.main_bottom_setting:
                    ((ViewPager)findViewById(R.id.viewPager)).setCurrentItem(2, false);
                    break;
                default:
                    break;
            }
            return true;
        });
    }

    @Override
    protected void onDestroy() {
        SettingInstance.getInstance(this).getSharedPreferences().unregisterOnSharedPreferenceChangeListener(onSharedPreferenceChangeListener);
        super.onDestroy();
    }

    @Override
    int resLayout() {
        return R.layout.activity_main;
    }
}
